<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Centermod extends CI_Model 
{

    function __construct()
    {
        parent::__construct();
    	$this->load->database();

	}
	
//################## commun function################################
	public function updatetemplate($tablename,$data,$where)
		{
				
			$str = $this->db->update_string($tablename, $data, $where);
			
			$query = $this->db->query($str);
			//print_r($query);
			
		}	
		
	public function insertetemplate($tablename,$data)
		{
				
			$str = $this->db->insert_string($tablename, $data);
			$query = $this->db->query($str);
			$idinsert=$this->db->insert_id();
			return $idinsert;
			
		}	

	public  function getoption($code)
	{
		$query = $this->db->query("SELECT  * FROM options where code='$code' LIMIT 0, 1 ");
		$value_arr=$query->result_array();
		if(count($value_arr)==1)
		{
			return $value_arr[0]["content"];
		}else 
		
		return "";
	
		
	}	
		
//######################### end commun function###########################


	public  function getclientinfobyid($idclient)
		{
			$query = $this->db->query("SELECT * FROM clients where idclients=$idclient");
			return $query->result_array();
		}
	
	
		
	public function listallclientbyiduser($iduser)
		{
				$query=$this->db->query("
					SELECT * FROM casefollowups  as cf
					left join clients as cl on clients_idclients=idclients 
					left join status as st on st.idstatus=cf.status_idstatus 
					where user_iduser=$iduser
										");
				return $query->result_array();
		}
	
	public function listestatusclientbyidcase($idcase)
		{
			$query=$this->db->query("
					SELECT * FROM casefollowups as cf
					 inner join clients as c on  cf.clients_idclients =c.idclients
						inner join status as st on st.idstatus=cf.status_idstatus 
						 where idcase=$idcase
						");
				return $query->result_array();
		}
		
		
	public function statuslist()
		{
				$query=$this->db->query("select * from status ");	
				return $query->result_array();	
		}	
		
	public function forfaitlist()
	{
		$query=$this->db->query("select * from forfait");
		return $query->result_array();
	}
	
	public function getnameuser($iduser)
	{
		$query=$this->db->query("select name from User where iduser=$iduser");
		$arr_user=$query->result_array();
		return $arr_user[0]["name"];
	}
	
	public function getforfaitnamebyid($forfaitid)
		{
			$query=$this->db->query("select * from forfait where idforfait= $forfaitid");
			return $query->result_array();
		}
		
	public  function ckebasestore($idclient)
		{
			$query=$this->db->query("SELECT * 
					FROM stores where clients_idclients=$idclient 
					and name='base'");
			$arr_total=$query->result_array();
			if(count($arr_total)>=1)
			{
				return 1;
			}else 
		
			return 0;
		}
		
	public function getidstorebyidclient($idclient)	
		{
			$query=$this->db->query("SELECT * 
					FROM stores where clients_idclients=$idclient 
					and name='base'");
			$arr_total=$query->result_array();
	    if(count($arr_total)>=1)
			{
				
				return $arr_total[0]["idstores"];
			}else 
		
			return 0;
		}
	
	
	public function getuserappointbyuser($iduser)
	{
		$query=$this->db->query("select app.idappointments, app.idgooglecalendor, app.casefollowups_idcase, app.stores_idstores, app.coments, app.forfait_idforfait, app.date, app.starttime, app.endtime
,cf.reminder, cf.longtimefollowups,companyname, address, tele, fax, city, contactperson, contactpersonemail, ff.name as forfaitname, prix, ff.qtybull, ff.comments, ff.timeshow
from appointments as app inner join casefollowups as cf on cf.idcase= app.casefollowups_idcase inner join clients as cl on cl.idclients=cf.clients_idclients
inner join forfait as ff on ff.idforfait=app.forfait_idforfait where cf.User_idUser=$iduser order by  app.starttime desc ");
		
		return $query->result_array();
	}
	
	
//get appointment by idappointement

	public function getuserappointbyidpointement($idappointments)
	{
		$query=$this->db->query("select app.idappointments, app.idgooglecalendor, app.casefollowups_idcase, app.stores_idstores, app.coments, app.forfait_idforfait, app.date, app.starttime, app.endtime
,cf.reminder, cf.longtimefollowups,companyname, address, tele, fax, city, contactperson, contactpersonemail, ff.name as forfaitname, prix, ff.qtybull, ff.comments, ff.timeshow,u.name as username
from appointments as app inner join casefollowups as cf on cf.idcase= app.casefollowups_idcase inner join clients as cl on cl.idclients=cf.clients_idclients
inner join forfait as ff on ff.idforfait=app.forfait_idforfait inner join User as u on u.idUser= cf.User_idUser  where app.idappointments =$idappointments");
		
		return $query->result_array();
	}
	

	
/*** tous filtre ************/
		
 public function getlistclientbystatus($iduser,$statusid)
 {
 					$query=$this->db->query("
					SELECT * FROM casefollowups  as cf
					left join clients as cl on clients_idclients=idclients 
					left join status as st on st.idstatus=cf.status_idstatus 
					where user_iduser=? and st.idstatus=?",array($iduser,$statusid));
										
				return $query->result_array();
}
  
public function getlclientbylongdatereminder($iduser)
 {
 					$query=$this->db->query("
					SELECT * FROM casefollowups  as cf
					left join clients as cl on clients_idclients=idclients 
					left join status as st on st.idstatus=cf.status_idstatus 
					where user_iduser=? and (cf.reminderlongtime>=now() and cf.reminderlongtime<=now() + INTERVAL 7 DAY) ",array($iduser));
										
				return $query->result_array();
}
}
